/*
 * MIT License
 *
 * Copyright (c) 2024 恩爸编程
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.enba.boot.mongodb;

import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

public class MongoOperator {

  private final MongoTemplate mongoTemplate;

  public MongoOperator(MongoTemplate mongoTemplate) {
    this.mongoTemplate = mongoTemplate;
  }

  // 插入操作
  public <T> T insert(T object) {
    return mongoTemplate.insert(object);
  }

  // 批量插入操作
  public <T> Collection<T> insertBatch(List<T> objectList) {
    return mongoTemplate.insertAll(objectList);
  }

  // 根据ID查询
  public <T> T findById(Class<T> entityClass, String id) {
    return mongoTemplate.findById(id, entityClass);
  }

  // 根据条件查询单个对象
  public <T> T findOne(Class<T> entityClass, Criteria criteria) {
    Query query = Query.query(criteria);
    return mongoTemplate.findOne(query, entityClass);
  }

  // 根据条件查询多个对象
  public <T> List<T> find(Class<T> entityClass, Criteria criteria) {
    Query query = Query.query(criteria);
    return mongoTemplate.find(query, entityClass);
  }

  // 更新操作
  public <T> T update(Class<T> entityClass, String id, Update update) {
    Query query = Query.query(Criteria.where("_id").is(id));
    return mongoTemplate.findAndModify(query, update, entityClass);
  }

  // 删除操作
  public void delete(Class<?> entityClass, String id) {
    Query query = Query.query(Criteria.where("_id").is(id));
    mongoTemplate.remove(query, entityClass);
  }

  // 分页查询方法，支持不同集合不同条件分页查询
  public <T> Page<T> findPage(
      Class<T> entityClass, Criteria criteria, int page, int size, Sort sort) {
    Pageable pageable = PageRequest.of(page, size, sort);
    Query query = Query.query(criteria).with(pageable);
    return null;
  }
}
